package com.letian.web.controller.im;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.letian.im.domain.model.UserMessagesVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.letian.common.annotation.Log;
import com.letian.common.core.controller.BaseController;
import com.letian.common.core.domain.AjaxResult;
import com.letian.common.enums.BusinessType;
import com.letian.im.domain.ImMessage;
import com.letian.im.service.IImMessageService;
import com.letian.common.utils.poi.ExcelUtil;
import com.letian.common.core.page.TableDataInfo;

/**
 * 聊天信息Controller
 * 
 * @author bmz
 * @date 2024-01-28
 */
@RestController
@RequestMapping("/im/message")
public class ImMessageController extends BaseController
{
    @Autowired
    private IImMessageService imMessageService;

    /**
     * 查询聊天信息列表
     */
    @PreAuthorize("@ss.hasPermi('im:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(ImMessage imMessage)
    {
        startPage();
        List<ImMessage> list = imMessageService.selectImMessageList(imMessage);
        return getDataTable(list);
    }

    /**
     * 聊天信息
     *
     * @param toId   用户
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('im:message:list')")
    @GetMapping("/queryMessageByFromToUserId/{toId}")
    public UserMessagesVO queryMessageByFromToUserId(@PathVariable("toId") Long toId){
        Long fromId = getUserId();
        return  imMessageService.queryMessageByFromToUserId(fromId, toId);
    }

    /**
     * 导出聊天信息列表
     */
    @PreAuthorize("@ss.hasPermi('im:message:export')")
    @Log(title = "聊天信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ImMessage imMessage)
    {
        List<ImMessage> list = imMessageService.selectImMessageList(imMessage);
        ExcelUtil<ImMessage> util = new ExcelUtil<ImMessage>(ImMessage.class);
        util.exportExcel(response, list, "聊天信息数据");
    }

    /**
     * 获取聊天信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('im:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(imMessageService.selectImMessageById(id));
    }

    /**
     * 新增聊天信息
     */
    @PreAuthorize("@ss.hasPermi('im:message:add')")
    @Log(title = "聊天信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ImMessage imMessage)
    {
        imMessage.setCreateBy(getUsername());
        imMessage.setUpdateBy(getUsername());
        return toAjax(imMessageService.insertImMessage(imMessage));
    }

    /**
     * 修改聊天信息
     */
    @PreAuthorize("@ss.hasPermi('im:message:edit')")
    @Log(title = "聊天信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ImMessage imMessage)
    {
        imMessage.setUpdateBy(getUsername());
        return toAjax(imMessageService.updateImMessage(imMessage));
    }

    /**
     * 删除聊天信息
     */
    @PreAuthorize("@ss.hasPermi('im:message:remove')")
    @Log(title = "聊天信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(imMessageService.deleteImMessageByIds(ids));
    }
}
